﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using Sale_Model;
using SaleSystem;
using ExtAspNet;
namespace SaleSystem.Fina
{
    public partial class frm_AccountRecordEdit : PageBase
    {
        #region Page Event
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControl();
                BindDropDownList();

                GetAndBind();
                
            }
        }
        protected void bnSave_Click(object sender, EventArgs e)
        {

            SModel.CreateMan = LinkService.GetCurrentUser().SUser_Id;
            SModel.CreateTime = System.DateTime.Now;
           
            SModel.AReco_Code = this.tb_AReco_Code.Text;
            SModel.AReco_Datetime = DateTime.Parse( this.dp_AReco_Man.Text);
            SModel.AReco_Kind = 1;
            if (this.ddl_AReco_InNumber.Hidden == false)
            {
                SModel.AReco_InNumber = ValueHandler.GetIntNumberValue(this.ddl_AReco_InNumber.SelectedValue);
                if (SModel.AReco_InNumber == -1)
                {
                    ExtAspNet.Alert.Show("请选择转入账户！","操作提示！", MessageBoxIcon.Information);
                    return;
                }
            }
            else
                SModel.AReco_InNumber = -1;
            if (this.ddl_AReco_OutNumber.Hidden == false)
            {
                SModel.AReco_OutNumber = ValueHandler.GetIntNumberValue(this.ddl_AReco_OutNumber.SelectedValue);
                if (SModel.AReco_OutNumber == -1)
                {
                    ExtAspNet.Alert.Show("请选择转出账户！", "操作提示！", MessageBoxIcon.Information);
                    return;
                }
                
            }
            else
                SModel.AReco_OutNumber = -1;
            SModel.AReco_Man = ValueHandler.GetIntNumberValue(this.ddl_BBill_SUser_Id.SelectedValue);
            SModel.AReco_Noney = ValueHandler.GetDecNumberValue(this.nb_AReco_Noney.Text);
            SModel.AReco_Reason = this.tb_AReco_Reason.Text;
            SModel.AReco_Type = ValueHandler.GetIntNumberValue( this.ddl_AReco_Type.SelectedValue );
            SModel.State = 1;
            SModel.ARecoId = Id;
            bool blHasExists = Manager.HasExists(SModel);
            if (blHasExists)
            {
                ExtAspNet.Alert.Show("该交易单号已经存在，请重新输入！", MessageBoxIcon.Information);
            }
            else
            {
                bool blRes = Manager.Save(SModel);
                if (blRes)
                {
                    //ExtAspNet.PageContext.RegisterStartupScript("parent.__doPostBack('','"+LinkService.RefreshString+"')");
                    // ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.
                    //ExtAspNet.Alert.Show("OK","OK", ExtAspNet.ActiveWindow.GetHidePostBackReference(LinkService.RefreshString));
                    ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference(LinkService.RefreshString));

                }
                else
                    ExtAspNet.Alert.Show("账户编辑失败！", MessageBoxIcon.Error);
            }
        }
        protected void ddl_AReco_Type_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetDropDownState();
        }
        #endregion

        #region Method
        private void InitControl()
        {
            bnClose.OnClientClick = ExtAspNet.ActiveWindow.GetHideReference();
            if (Request["Id"] != null)
            {
                Id = ValueHandler.GetIntNumberValue(Request["Id"]);
            }

        }
        private void SetDropDownState()
        {
            this.ddl_AReco_InNumber.Hidden = true;
            this.ddl_AReco_OutNumber.Hidden = true;
            if (this.ddl_AReco_Type.SelectedValue == "-1")
            {
                this.ddl_AReco_InNumber.Hidden = false;
                this.ddl_AReco_OutNumber.Hidden = false;
            }
            else if (this.ddl_AReco_Type.SelectedValue == "1")
            {
                this.ddl_AReco_InNumber.Hidden = false;
            }
            else if (this.ddl_AReco_Type.SelectedValue == "2")
            {
                this.ddl_AReco_OutNumber.Hidden = false;
            }
            else
            {
                this.ddl_AReco_InNumber.Hidden = false;
                this.ddl_AReco_OutNumber.Hidden = false;
            }
        }
        private void BindDropDownList()
        {

            DataTable dtType = Manager.GetView_RecordType();
            ddl_AReco_Type.DataSource = dtType;
            ddl_AReco_Type.DataBind();
            ddl_AReco_Type.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));

            //账户信息获取
            AccountListOp ListOp = new AccountListOp();
            DataTable dtAccountList = ListOp.GetList("");
            this.ddl_AReco_OutNumber.DataSource = dtAccountList;
            this.ddl_AReco_OutNumber.DataBind(); ;
            this.ddl_AReco_OutNumber.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
            this.ddl_AReco_InNumber.DataSource = dtAccountList;
            this.ddl_AReco_InNumber.DataBind();
            this.ddl_AReco_InNumber.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));

            SysUsersOp userManager = new SysUsersOp();
            DataTable dtUser = userManager.GetList("");
            this.ddl_BBill_SUser_Id.DataSource = dtUser;
            this.ddl_BBill_SUser_Id.DataBind();
            this.ddl_BBill_SUser_Id.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
            this.ddl_BBill_SUser_Id.SelectedValue = LinkService.GetCurrentUser().SUser_Id.ToString();

        }
        private void GetAndBind()
        {
            if (Id != 0)
            {
                SModel.ARecoId = Id;
                SModel = Manager.GetModel(SModel) as AccountRecord;
                this.tb_AReco_Code.Text = SModel.AReco_Code;
                this.tb_AReco_Reason.Text = SModel.AReco_Reason;
                this.nb_AReco_Noney.Text = SModel.AReco_Noney.ToString();
                this.ddl_AReco_InNumber.SelectedValue = SModel.AReco_InNumber.ToString();
                this.ddl_AReco_OutNumber.SelectedValue = SModel.AReco_OutNumber.ToString();
                this.ddl_AReco_Type.SelectedValue = SModel.AReco_Type.ToString();
                this.ddl_BBill_SUser_Id.SelectedValue = SModel.AReco_Man.ToString();
                this.dp_AReco_Man.Text = SModel.AReco_Datetime.ToString("yyyy-MM-dd");
                this.ddl_AReco_Type.Enabled = false;
                SetDropDownState();
            }
            else
            {
                this.tb_AReco_Code.Text = Coder.CreateCoder(EMenuList.转账管理);
                this.dp_AReco_Man.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
            }

        }
        #endregion

        #region Property
        AccountRecordOp Manager = new AccountRecordOp();

        new Sale_Model.AccountRecord SModel = new Sale_Model.AccountRecord();
        #endregion
    }
}
